﻿using AutoMapper;
using MediatR;
using Production_Set.API.Application.Commands.Orderers;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Order;
using THE_WIND_RISES.Common.Domans.Warehousing;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Warehousings
{
    public class OrderTineCreateHandler : IRequestHandler<OrderTineCreateCommend, Result>
    {
        private readonly IBaseRepository<OrderTine> OrderTine;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="Resource"></param>
        public OrderTineCreateHandler(IMapper mapper, IBaseRepository<OrderTine> OrderTine)
        {
            this.mapper = mapper;
            this.OrderTine = OrderTine;
        }

        public async Task<Result> Handle(OrderTineCreateCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Added };

            var AddData = mapper.Map<OrderTine>(request);
            string sql = "insert into OrderTine values(@OrderTineId,@InventoryInfoId,@Prices,@Nums,@Unit,@Summary,@StorageId,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid);";
            var AddNum = OrderTine.Add(sql, new
            {
                OrderTineId = AddData.OrderTineId,
                InventoryInfoId = AddData.InventoryInfoId,
                Prices = AddData.Prices,
                Nums = AddData.Nums,
                Unit = AddData.Unit,
                Summary = AddData.Summary,
                StorageId = AddData.StorageId,
                IsDelete = AddData.IsDelete,
                Handle = AddData.Handle = "李四",
                HandleContext = AddData.HandleContext = "添加",
                HandleTime = AddData.HandleTime = DateTime.Now,
                Pid = AddData.Pid = 0,
            });
            if (AddNum > 0)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "添加成功";
                return results;
            }
            else
            {
                return results;
            }
        }
       }
}
